<?php
namespace Home\Controller;

use Util\AccessController;

class CardController extends AccessController {

    /*
     * 获取优惠劵
     * post
     * USER_ID 用户id
     * (有订单价格就获取可用优惠劵，没有就是用户全部优惠劵)MONEY 订单价格
     */
    public function GetMyCouponCard() {
        $where['ts_card_log.USER_ID'] = $_POST['USER_ID'];
        //获取用户可使用优惠劵
        if ($_POST['MONEY']) {
            $where['ts_card.BJ_MONEY'] = array('ELT', $_POST['MONEY']);
            $where['ts_card_log.STATE'] = 1;
            $where['ts_card.ETIME'] = array('EGT', date('Y-m-d'));
            $where['ts_card.STIME'] = array('ELT', date('Y-m-d'));
        }

        $where['ts_card.STATE'] = 2;

        $LS = Service('Card', 'card_log');
        $files = 'ts_card.*,ts_card_log.STATE as isUse,ts_card_log.ID';
        $data = $LS->query($where, null, $files, null, 'ts_card on ts_card_log.CARD_ID = ts_card.ID');
        $data = $this->SetCardState($data);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 设置优惠劵状态
     */
    function SetCardState($data) {
        foreach ($data as $k => $v) {
            if ($v['isUse'] == 1) {
                $data[$k]['STATES'] = '未使用';
                if ($v['ETIME'] < date('Y-m-d')) {
                    $data[$k]['STATES'] = '过期了';
                }
                if ($v['STIME'] > date('Y-m-d')) {
                    $data[$k]['STATES'] = '还没开始';
                }
            } else {
                $data[$k]['STATES'] = '已使用';
            }
            unset($data[$k]['isUse']);
            unset($data[$k]['STATE']);
        }
        return $data;
    }

}